using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace ConfigurationSettings
{
    public class ApplicationSettings : IApplicationSettings
    {

        private  string _path = String.Empty;
        private  FileTrackerConfiguration _configuration;

        #region ** Properties **

        #region *** Other Properties ***

        public  Version AssemblyVersion
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }

                    return _configuration.AssemblyVersion;
            }
        }

        public  Point MainWindowLocation
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }

                return _configuration.MainWindowLocation;
            }

            set
            {
                Point thisLocation = value;

                //Make sure the window is not off the left of the screen
                if (value.X < 0)
                {
                    thisLocation.X = 0;
                }
                //Make sure the window is not off the right of the screen
                if (value.X > Screen.PrimaryScreen.WorkingArea.Right)
                {
                    thisLocation.X = 0;
                }
                //Make sure the window is not off the top of the screen
                if (value.Y < 0)
                {
                    thisLocation.Y = 0;
                }
                //Make sure the window is not off the bottom of the screen
                if (value.Y > Screen.PrimaryScreen.WorkingArea.Bottom)
                {
                    thisLocation.Y = 0;
                }

                _configuration.MainWindowLocation = thisLocation;
            }
        }

        public  Size MainWindowSize
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }

                return _configuration.MainWindowSize;
            }
            set
            {
                if (value.Width < 733)
                    value.Width = 733;

                if (value.Height < 221)
                    value.Height = 221;

                _configuration.MainWindowSize = value;
            }
        }

        public  int MainSplitterDistance
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }

                return _configuration.MainSplitterDistance;
            }

            set
            {
                _configuration.MainSplitterDistance = value;
            }
        }

        public  Point PreferenceWindowLocation
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.PreferenceWindowLocation;
            }
            set
            {
                _configuration.PreferenceWindowLocation = value;
            }
        }

        public  Size PreferenceWindowSize
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.PreferenceWindowSize;
            }
            set
            {
                _configuration.PreferenceWindowSize = value;
            }
        }

        public  int PreferencesSplitterDistance
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.PreferencesSplitterDistance;
            }
            set
            {
                _configuration.PreferencesSplitterDistance = value;
            }
        }

        public  SizeUnitType SizeUnit
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.SizeUnit;
            }
            set
            {
                _configuration.SizeUnit = value;
            }
        }

        public  int SizePrecision
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.SizePrecision;
            }
            set
            {
                _configuration.SizePrecision = value;
            }
        }

        #endregion

        #region *** Colors ***

        public  Color FocusColor
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.FocusColor;
            }

            set
            {
                _configuration.FocusColor = value;
            }
        }

        public  Color ListViewBackColor
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.ListViewBackColor;
            }

            set
            {
                _configuration.ListViewBackColor = value;

            }
        }

        public  ItemSettings FileSettings
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.FileSettings;
            }
            set
            {
                _configuration.FileSettings = value;
            }
        }

        public  ItemSettings DirectorySettings
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.DirectorySettings;
            }
            set
            {
                _configuration.DirectorySettings = value;
            }
        }

        public  ColumnExtendedProperties ColumnSettings
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.ColumnSettings;
            }
            set
            {
                _configuration.ColumnSettings = value;
            }
        }

        #endregion

        #region ** General FileTrackerConfiguration **

        public  bool AskBeforeDelete
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.AskBeforeDelete;
            }

            set
            {
                _configuration.AskBeforeDelete = value;

            }
        }

        public  bool LeftAutoSort
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.LeftAutoSort;
            }
        }

        public  bool RightAutoSort
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.RightAutoSort;
            }
        }

        public  string LeftDefaultFolder
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.LeftDefaultFolder;
            }

            set
            {
                _configuration.LeftDefaultFolder = value;

            }
        }

        public  FolderType LeftDefaultFolderType
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.LeftDefaultFolderType;
            }

            set
            {
                _configuration.LeftDefaultFolderType = value;
            }
        }

        public  string RightDefaultFolder
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.RightDefaultFolder;
            }

            set
            {
                _configuration.RightDefaultFolder = value;
            }
        }

        public  FolderType RightDefaultFolderType
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.RightDefaultFolderType;
            }

            set
            {
                _configuration.RightDefaultFolderType = value;
            }

        }

        public  Column LeftDefaultSortColumn
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.LeftDefaultSortColumn;
            }

            set
            {
                _configuration.LeftDefaultSortColumn = value;
            }
        }

        public  Column RightDefaultSortColumn
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.RightDefaultSortColumn;
            }

            set
            {
                _configuration.RightDefaultSortColumn = value;
            }
        }

        #endregion

        #region ** Favorites and OpenWith **

        public  Dictionary<string, string> FavoriteList
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.FavoriteList;
            }
        }

        public  Dictionary<string, string> ProgramList
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.ProgramList;
            }
        }

        public  Dictionary<KeyEnum, Keys> KeyList
        {
            get
            {
                if (_configuration == null)
                {
                    _configuration = Reload();
                }
                return _configuration.KeyList;
            }
        }

        #endregion

        #endregion

        #region ** Constructor / Destructor **

        public ApplicationSettings()
        {
            {
                string appPath = Application.StartupPath;

                DirectoryInfo configPath = new DirectoryInfo(appPath);
                _path = configPath.FullName + @"\FileTrackerConfig.config";
            }
        }

        #endregion

        #region ** Public Methods **

        private   FileTrackerConfiguration Reload()
        {
            {
                FileTrackerConfiguration localConfig;

                FileInfo testFile = new FileInfo(_path);

                if ((testFile.Exists) && (testFile.Length > 0))
                {

                    BinaryFormatter binFormatter = new BinaryFormatter();

                    try
                    {
                        using (Stream serializationStream = File.Open(_path, FileMode.Open))
                        {
                            localConfig = (FileTrackerConfiguration)binFormatter.Deserialize(serializationStream);
                        }
                    }
                    catch (SerializationException)
                    {
                        //If there's a problem with serialization then just make a new Config object.
                        localConfig = new FileTrackerConfiguration();
                        Save();
                    }
                }
                else
                {
                    localConfig = new FileTrackerConfiguration();
                }

                //if (localConfig.AssemblyVersion == null)
                //{
                //    localConfig = new FileTrackerConfiguration();
                //}

                return localConfig;
            }
            //Get the current assembly version
            //TempVersion = Assembly.GetExecutingAssembly().GetName().Version;
            //testVersion = new Version(TempVersion.Major, TempVersion.Minor, TempVersion.Build, 0);

            //if (_Configuration.AssemblyVersion < testVersion)
            //{
            //    Upgrade();
            //}

        }

        public  void Save()
        {
            {
                if (_configuration == null)
                {
                    _configuration = new FileTrackerConfiguration();
                }

                using (Stream serializationStream = File.Open(_path, FileMode.Create))
                {
                    BinaryFormatter binFormatter = new BinaryFormatter();

                    binFormatter.Serialize(serializationStream, _configuration);
                }
            }
        }

        //private void Upgrade()
        //{
        //    ConfigurationBase TempConfig;
        //    string ConfigVersion;
        //    Version testVersion;
        //    Version TempVersion;

        //    //using (new Microsoft.Practices.EnterpriseLibrary.Logging.Tracer("ApplicationSettings"))
        //    {
        //        //Deserialise as ConfigBase
        //        Stream stream = File.Open(_path, FileMode.Open);

        //        BinaryFormatter formatter = new BinaryFormatter();

        //        try
        //        {
        //            TempConfig = (ConfigurationBase)formatter.Deserialize(stream);
        //            stream.Close();
        //        }
        //        catch (Exception)
        //        {
        //            _Configuration = new FileTrackerConfiguration();
        //            return;
        //        }

        //        //Check the version number
        //        TempVersion = Assembly.GetEntryAssembly().GetName().Version;
        //        testVersion = new Version(TempVersion.Major, TempVersion.Minor, TempVersion.Build, 0);

        //        ConfigVersion = TempConfig.AssemblyVersion.ToString();
        //        switch (ConfigVersion)
        //        {
        //            case "":
        //                Upgrade();
        //                break;
        //        }
        //        //Create the correct version
        //        //Upgrade the old version
        //        //Save the new version


        //        return;
        //    }
        //}
		
		/**/

        //private  void Reset()
        //{
        //    _configuration = null;

        //    _configuration = new FileTrackerConfiguration();
        //}

        #endregion
    }
}
